package com.zls.controller;

import com.zls.dto.DeveloperDto;
import com.zls.page.PageResult;
import com.zls.page.QueryPageBean;
import com.zls.page.Result;
import com.zls.pojo.Developer;
import com.zls.service.DeveloperService;
import com.zls.vo.DevProVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/developer")
@RestController
public class DeveloperController {
    @Autowired
    private DeveloperService developerService;

    @GetMapping("findAll")
    public List<Developer> findAll(){
        List<Developer> all = developerService.findAll();
        return all;
    }

    /**
     * 分页查询
     */
    @PostMapping("all")
    public PageResult all(@RequestBody QueryPageBean queryPageBean){
        PageResult page = developerService.findPage(queryPageBean);
        return page;
    }

    /**
     * 开发商添加
     */
    @PostMapping("add")
    public Result add(@RequestBody DevProVo devProVo){
        int i = developerService.addAndBind(devProVo.getDname(),devProVo.getPids());
        if (i>0){
            return Result.success();
        }else {
            return Result.fail();
        }
    }
    /**
     * 开发商删除
     */
    @GetMapping("delete")
    public Result delete(@RequestParam("did")String did){
        int i = developerService.deleteDeve(did);
        Result result=new Result();
        if (i>0){
            result.setMessage("删除成功");
            result.setFlag(true);
        }else {
            result.setMessage("删除失败");
        }
        return result;
    }

    /**
     * 查询修改开发商
     */
    @GetMapping("findByDid")
    public Result findByDid(@RequestParam("did") String did){
        DeveloperDto developerDto = developerService.findByDid(did);
        Result result=new Result();
        if (developerDto!=null){
            result.setData(developerDto);
            result.setFlag(true);
            result.setMessage("success");
        }else {
            result.setMessage("系统错误");
        }
        return result;
    }
    /**
     * 修改开发商
     */
    @PostMapping("edit")
    public Result edit(@RequestBody DevProVo devProVo){
        int i = developerService.editDeve(devProVo);
        Result result=new Result();
        if (i>0){
            result.setMessage("修改成功");
            result.setFlag(true);
        }else {
            result.setMessage("修改失败");
        }
        return result;
    }
}
